<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MX_Controller 
{
	public function __construct()
	{
		parent::__construct();		
		$this->display->set_theme('cms');		
	}
	
	public function index()
	{	
		permission('cms', 'user_list');
		$users = $this->ion_auth->get_users();
		$groups = $this->ion_auth->groups()->result_array();
		$this->twiggy->title()->append('Users');
		$this->display->template('user_list', array('users' => $users, 'groups' => $groups));
	}
	
	public function disable($id)
	{		
		permission('cms', 'user_enable');
		
		if(!ctype_digit($id))
			cms_redirect('users');
		
		$group = $this->ion_auth->get_users_group($id)->row_array();
		if($group['name'] == $this->config->item('admin_group') && group('name') != $this->config->item('admin_group'))
		{
			flash_message('error', 'You are not allowed to disable someone from the admin group');
			cms_redirect('users');
		}
		
		$updated = $this->ion_auth->update($id, array('active' => 0));
		
		if($updated)
			flash_message('success', 'User disabled');
		else
			flash_message('error', 'Could not disable user');
		
		cms_redirect('users');		
	}
	public function enable($id)
	{		
		permission('cms', 'user_enable');
		
		if(!ctype_digit($id))
			cms_redirect('users');
			
		$group = $this->ion_auth->get_users_groups($id)->row_array();
		if($group['name'] == $this->config->item('admin_group') && group('name') != $this->config->item('admin_group'))
		{
			flash_message('error', 'You are not allowed to enable someone from the admin group');
			cms_redirect('users');
		}
					
		$updated = $this->ion_auth->update($id, array('active' => 1));
		
		if($updated)
			flash_message('success', 'User enabled');
		else
			flash_message('error', 'Could not enable users');
		
		cms_redirect('users');
	}
	
	public function edit($id)
	{
		permission('cms', 'user_edit');
		
		if(!ctype_digit($id))
			cms_redirect('users/edit/'.$id);
		
		$group = $this->ion_auth->get_users_groups($id)->row_array();
		if($group['name'] == $this->config->item('admin_group') && group('name') != $this->config->item('admin_group'))
		{
			flash_message('error', 'You are not allowed to edit someone from the admin group');
			cms_redirect('users');
		}		
			
		$user = $this->ion_auth->user($id)->row_array();
		$groups = $this->ion_auth->groups()->result_array();
		$group = $this->ion_auth->get_users_groups($id)->row_array();
		
		
		if($this->input->post())
		{
			$this->load->library('form_validation');
			$valid = $this->form_validation->run('edit_user');
			if($valid)
			{
				$update = array('username' 		=> $this->input->post('username'),
								'email' 		=> $this->input->post('email'),
								'first_name'	=> $this->input->post('first_name'),
								'last_name'		=> $this->input->post('last_name'));
				
				$update['active'] = 0;
				if($this->input->post('enabled'))
					$update['active'] = 1;
					
				if($this->input->post('password'))
					$update['password'] = $this->input->post('password');
				
				
				$updated = $this->ion_auth->update($id, $update);
				
				if($this->input->post('group') != $group['id'])
				{
					$this->ion_auth->remove_from_group(FALSE, $user['id']);
					$this->ion_auth->add_to_group($this->input->post('group'), $user['id']);
				}
				
				flash_message('success', 'User has been updated');
			}
			else
			{
				flash_message('error', validation_errors());
			}
			cms_redirect('users/edit/' . $id);
		}
		else
		{			
			$this->twiggy->title()->append($user['username']);
			$this->display->template('user_edit', array('user' => $user, 'groups' => $groups, 'group' => $group));
		}
		
	}
	
	public function delete_group($id)
	{
		permission('cms', 'group_edit');
		
		if(!ctype_digit($id))
			cms_redirect('users');		
			
		$group = $this->ion_auth->group($id)->row_array();
		if($group['name'] == config_item('default_group') || $group['name'] == config_item('admin_group'))
		{
			flash_message('error', 'You can not remove the default or admin group');
			cms_redirect('users');		
		}
		else
		{
			$this->ion_auth->delete_group($id);
			flash_message('success', 'Removed group "' . $group['name'] . '", accounts moved to default group');
			cms_redirect('users');
		}
		
	}
	
	public function edit_group($id)
	{
		permission('cms', 'group_edit');
		
		if(!ctype_digit($id))
			cms_redirect('users');
			
		$group = $this->ion_auth->group($id)->row_array();
		if(empty($group))
		{
			flash_message('error', 'Group does not exists');
			cms_redirect('users');
		}
		
		
		if($group['name'] == config_item('admin_group') && group('name') != config_item('admin_group') )
		{
			flash_message('error', 'Can not edit admin group');
			cms_redirect('users');
		}
		
		
		$this->twiggy->title()->append('Edit group');
		$this->display->template('group_edit');
		
	}
	
	public function default_group($id)
	{
		permission('cms', 'group_edit');
		
		if(!ctype_digit($id))
			cms_redirect('users');		
			
		$this->load->model('config_model');
		$group = $this->ion_auth->group($id)->row_array();
		$this->config_model->set_item('default_group', $group['name']);
		
		flash_message('success', 'You have set the default group to: ' . $group['name']);
		cms_redirect('users');
	}
	
	public function admin_group($id)
	{
		if( ! $this->ion_auth->is_admin() )
			show_error('You are not allowed to do this', 403);
					
		if(!ctype_digit($id))
			cms_redirect('users');		
			
		$this->load->model('config_model');
		$group = $this->ion_auth->group($id)->row_array();
		$this->config_model->set_item('admin_group', $group['name']);
		
		flash_message('success', 'You have set the admin group to: ' . $group['name']);
		cms_redirect('users');
	}
	
	public function create_group()
	{
		permission('cms', 'group_edit');
		
		if($this->input->post())
		{
			$this->load->library('form_validation');
			$valid = $this->form_validation->run('create_group');
			if($valid)
			{
				$this->ion_auth->create_group($this->input->post('name'), $this->input->post('description'));
				flash_message('success', 'Group has been created');				
			}
			else
			{
				flash_message('error', validation_errors());
				
			}
			cms_redirect('users');
		}
	}
	
	// Ajax function
	public function toggle_permission($id, $permission)
	{
		if( ! $this->ion_auth->is_admin() )
			show_error('You are not allowed to do this', 403);
			
		if(!ctype_digit($id))
			cms_redirect('users');
			
		$permissionData = $this->db->select($permission)->from('groups')->where('id', $id)->get();
		if($permissionData->num_rows() == 1)
		{
			$permissionData = $permissionData->row_array();
			$value = $permissionData[$permission];
			$newValue = 0;
			if($value == 0)
				$newValue = 1;
			
			$this->db->where('id', $id)->update('groups', array($permission => $newValue));
			$this->display->array_json(array('success' => TRUE));	
		}
	}
	
	public function create(){
		
		$suc = $this->ion_auth->register('Ynnah', 'test', 'sau@live.nl', array('first_name' => 'Hanny', 'last_name' => 'Fok'), array('Member'));
		if($suc)
			echo 'suc';
		}
	

	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */